{% extends '__base__.html' %}
{% load staticfiles %}

{% block content %}
    {% load group %}

    {#        表格有数据时展示#}
    <div class="app-title">
        {#            页面简介区域#}
        <div>
            <h1><i class="fa fa-th-list"></i> 发布申请管理</h1>
            {% if apply_manage %}
                <p> 填写、审核发布申请单，与发布相关的配置下发、SLB移除等操作</p>
            {% else %}
                <p>No apply are available.</p>
            {% endif %}
        </div>
        {#            面包屑区域#}
        <ul class="app-breadcrumb breadcrumb side">
            <li class="breadcrumb-item"><i class="fa fa-home fa-lg"></i></li>
            <li class="breadcrumb-item active">发布申请管理</li>
        </ul>
    </div>

    {#        按钮区域#}
    <div>
        <a class="btn btn-primary" type="button" href="{% url 'deploy:apply_create' %}">新增发布申请</a>
    </div>

    {% if apply_manage %}
        {#        表格区域#}
        <div class="row" id="loading">
            <div class="col-md-12">
                <div class="tile">
                    {#                    表格数据区域#}
                    <div class="tile-body table-responsive" id="table">
                        <table class="table table-hover table-bordered" id="sampleTable"
                               style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
                            <thead>
                            <tr>
                                <th>申请编号</th>
                                <th>状态</th>
                                <th>申请人</th>
                                <th>申请时间</th>
                                <th>期望发布时间</th>
                                <th>发布人</th>
                                <th>实际发布时间</th>
                                <th class="text-center">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            {% for apply in apply_manage %}
                                <tr>
                                    <td>
                                        <a href="{% url 'deploy:apply_update' apply.id %}">{{ apply.apply_project }}</a>
                                    </td>
                                    <td>
                                        {% if apply.status == "WC" %}
                                            <span>待提交</span>
                                        {% elif apply.status == "WD" %}
                                            <span>待发布</span>
                                        {% elif apply.status == "C" %}
                                            <span>已取消</span>
                                        {% elif apply.status == "D" %}
                                            <span>已发布</span>
                                        {% elif apply.status == "DA" %}
                                            <span>研发经理审批中</span>
                                        {% elif apply.status == "OA" %}
                                            <span>运维经理审批中</span>
                                        {% elif apply.status == "EA" %}
                                            <span>运维工程师审批中</span>
                                        {% elif apply.status == "TA" %}
                                            <span>测试经理审批中</span>
                                        {% elif apply.status == "TDA" %}
                                            <span>技术总监审批中</span>
                                        {% endif %}
                                    </td>
                                    <td>
                                        {{ apply.apply_user }}
                                    </td>
                                    <td>
                                        {{ apply.apply_time|date:"Y-m-d H:i:s" }}
                                    </td>
                                    <td>
                                        {% if apply.wish_deploy_time is null %}
                                        {% else %}
                                            {{ apply.wish_deploy_time|date:"Y-m-d" }}
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if apply.deploy_user is null %}
                                        {% else %}
                                            {{ apply.deploy_user }}
                                        {% endif %}
                                    </td>
                                    <td>
                                        {% if apply.deploy_time is null %}
                                        {% else %}
                                            {{ apply.deploy_time }}
                                        {% endif %}
                                    </td>
                                    <td class="text-right">
                                        <!--修改按钮-->
                                        {% if apply.status == "WC" %}
                                            <a class="btn btn-success"
                                               href="{% url 'deploy:apply_update' apply.id %}">修改</a>
                                        {% endif %}
                                        <!--审核按钮-->
                                        {% if apply.status == "DA" %}
                                            {% if request.user.is_superuser or request.user|has_group:"admin" or request.user|has_group:"研发经理" %}
                                                <a class="btn btn-primary"
                                                   href="">研发经理审核</a>
                                            {% endif %}
                                        {% endif %}
                                        {% if apply.status == "TA" %}
                                            {% if request.user.is_superuser or request.user|has_group:"admin" or request.user|has_group:"测试经理" %}
                                                <a class="btn btn-primary"
                                                   href="">测试经理审核</a>
                                            {% endif %}
                                        {% endif %}
                                        {% if apply.status == "EA" %}
                                            {% if request.user.is_superuser or request.user|has_group:"admin" or request.user|has_group:"运维工程师" %}
                                                <a class="btn btn-primary"
                                                   href="">运维工程师审核</a>
                                            {% endif %}
                                        {% endif %}
                                        {% if apply.status == "OA" %}
                                            {% if request.user.is_superuser or request.user|has_group:"admin" or request.user|has_group:"运维经理" %}
                                                <a class="btn btn-primary"
                                                   href="">运维经理审核</a>
                                            {% endif %}
                                        {% endif %}
                                        {% if apply.status == "TDA" %}
                                            {% if request.user.is_superuser or request.user|has_group:"admin" or request.user|has_group:"技术总监" %}
                                                <a class="btn btn-primary"
                                                   href="">技术总监审核</a>
                                            {% endif %}
                                        {% endif %}
                                        <!--现在去发布按钮-->
                                        {% if request.user.is_superuser or request.user|has_group:"运维工程师" or request.user|has_group:"admin" %}
                                            {% if apply.status == 'WD' %}
                                                <a class="btn btn-info"
                                                   href="">现在去发布</a>
                                            {% endif %}
                                        {% endif %}
                                        <!--删除按钮-->
                                        {% if request.user.is_superuser or request.user|has_group:"admin" %}
                                            <a class="btn btn-danger" id="demoSwal{{ forloop.counter }}" href="#">删除</a>
                                        {% endif %}
                                    </td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    {% else %}
        <div class="row">
            <div class="col-md-12">
                <div class="tile">
                    <div class="tile-body">请填写发布申请单</div>
                </div>
            </div>
        </div>
    {% endif %}

{% endblock %}

{% block custom_static %}

    <!-- Data table plugin-->
    <script type="text/javascript" src="{% static 'js/plugins/jquery.dataTables.min.js' %}"></script>
    <script type="text/javascript" src="{% static 'js/plugins/dataTables.bootstrap.min.js' %}"></script>
    <script type="text/javascript">$('#sampleTable').DataTable();</script>

    <!--Delete Alarm-->
    <script type="text/javascript" src="{% static 'js/plugins/sweetalert.min.js' %}"></script>
    <script type="text/javascript">
        $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}'},
        });
        {% for apply in apply_manage %}
            $('#demoSwal{{ forloop.counter }}').click(function () {
                swal({
                    title: "Are you sure?",
                    text: "You will not be able to recover this imaginary file!",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonText: "Yes, delete it!",
                    cancelButtonText: "No, cancel plx!",
                    closeOnConfirm: false,
                    closeOnCancel: false
                }, function (isConfirm) {
                    if (isConfirm) {
                        let postUrl = "{% url 'deploy:apply_delete' apply.id %}";
                        $.post(postUrl, function (data) {
                            if (data.success) {
                                swal("Deleted!", "Your imaginary file has been deleted.", "success");
                                location.reload();
                            }
                        })
                    } else {
                        swal("Cancelled", "Your imaginary file is safe :)", "error");
                    }
                });
            });
        {% endfor %}
    </script>

{% endblock %}